﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>乐优商城--商品搜索结果页</title>
    <link rel="icon" href="assets/img/favicon.ico">
    <link href='./css/material.css' rel="stylesheet">
    <link href="./css/vuetify.min.css" rel="stylesheet">
    <script src="./js/vue/vue.js"></script>
    <script src="./js/vue/vuetify.js"></script>
    <script src="./js/axios.min.js"></script>
    <script src="./js/common.js"></script>
    <link rel="stylesheet" type="text/css" href="css/webbase.css"/>
    <link rel="stylesheet" type="text/css" href="css/pages-list.css"/>
    <link rel="stylesheet" type="text/css" href="css/widget-cartPanelView.css"/>
    <style type="text/css">
        * {
            box-sizing: unset;
        }

        .btn-arrow, .btn-arrow:visited, .btn-arrow:link, .btn-arrow:active {
            width: 46px;
            height: 23px;
            border: 1px solid #DDD;
            background: #FFF;
            line-height: 23px;
            font-family: "\5b8b\4f53";
            text-align: center;
            font-size: 16px;
            color: #AAA;
            text-decoration: none;
            out-line: none
        }

        .btn-arrow:hover {
            background-color: #1299ec;
            color: whitesmoke;
        }

        .top-pagination {
            display: block;
            padding: 3px 15px;
            font-size: 11px;
            font-weight: 700;
            line-height: 18px;
            color: #999;
            text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
            text-transform: uppercase;
            float: right;
            margin-top: 6px
        }

        .top-pagination span {
            margin-right: 10px;
        }

                .logo-list li{
                    padding:8px;
                }
               .logo-list li:hover{
                    background-color: #f3f3f3;
                }
        .type-list a:hover {
            color: #1299ec;
        }

        .skus {
            list-style: none;
        }

        .skus li {
            list-style: none;
            display: inline-block;
            float: left;
            margin-left: 2px;
            border: 2px solid #f3f3f3;
        }

        .skus li.selected {
            border: 2px solid #dd1144;
        }

        .skus img {
            width: 25px;
            height: 25px;
        }

        .favorite {
            display: inline-block;
            padding: 2px 14px;
            border: 1px solid #e8351f;
            color: #fffcff;
            line-height: 18px;
            background-color: #ed6a5a;
            font-family: 微软雅黑;
        }
    </style>
    <script type="text/javascript" src="plugins/jquery/jquery.min.js"></script>
</head>

<body>

<div id="searchApp">
    <div id="nav-bottom">
        <ly-top/>
    </div>


    <!--list-content-->
    <div class="main">
        <div class="py-container">

            <div class="bread">
                <!--面包屑-->
                <ul class="fl sui-breadcrumb">
                    <li><span>全部结果:</span></li>
                    <li v-for="(c,i) in breads" :key="i"><a href="#" v-if="i < 2">{{c.name}}</a>
                        <span v-else>{{c.name}}</span>
                    </li>
                </ul>
                <!--已选择过滤项-->
                <ul class="tags-choose">
                    <li class="tag" v-for="(v,k) in search.filter" v-if="k !== 'cid3'" :key="k">
                        {{k==='brandId' ? '品牌':k}}<span style="color: red">{{getFilterValue(k,v)}}</span>

                        <i class="sui-icon icon-tb-close" @click="deleteFilter(k)"></i>
                    </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <!--selector-->
            <div class="clearfix selector">
                <div class="type-wrap" v-for="(f,i) in filters" :key="i" v-if="f.k !== '品牌'" v-show="i < 5 || show">
                    <div class="fl key">{{f.k}}</div>
                    <div class="fl value">
                        <ul class="type-list">
                            <li v-for="(option, j) in f.options" :key="j" @click="selectFilter(f.k,option)">
                                <a>{{option.name}}</a>
                            </li>
                        </ul>
                    </div>
                    <div class="fl ext"></div>
                </div>
                <div class="type-wrap logo" v-else>
                    <div class="fl key brand">{{f.k}}</div>
                    <div class="value logos">
                        <ul class="logo-list">

                            <li v-for="(option,j) in f.options" v-if="option.image" @click="selectFilter(f.k,option)">
                                <img :src="option.image">
                            </li>
                            <li style="text-align: center" v-else @click="selectFilter(f.k,option)">
                                <a style="line-height: 30px; font-size: 12px" href="#">{{option.name}}</a>
                            </li>
                        </ul>
                    </div>
                    <div class="fl ext">
                        <a href="javascript:void(0);" class="sui-btn">多选</a>
                    </div>

                </div>
                <div class="type-wrap" style="text-align: center">
                    <v-btn small flat @click="show = true" v-show="!show">
                        更多
                    </v-btn>
                    <v-btn small="" flat @click="show = false" v-show="show">
                        收起
                    </v-btn>
                </div>
            </div>
            <!--details-->
            <div class="details">
                <div class="sui-navbar">
                    <div class="navbar-inner filter">
                        <ul class="sui-nav">
                            <li :class="{active : !search.sortBy}" @click="search.sortBy=''">
                                <a href="#">综合</a>
                            </li>
                            <li>
                                <a href="#">销量</a>
                            </li>
                            <li :class="{active : search.sortBy=='createTime'}" @click="search.sortBy='createTime'">
                                <a href="#">新品</a>
                            </li>
                            <li>
                                <a href="#">评价</a>
                            </li>
                            <li :class="{active : search.sortBy=='price'}"
                                @click="search.sortBy='price' ; search.descending = !search.descending">
                                <a href="#">价格
                                    <v-icon v-show="search.descending">arrow_drop_down</v-icon>
                                    <v-icon v-show="!search.descending">arrow_drop_up</v-icon>
                                </a>
                            </li>
                        </ul>
                        <div class="top-pagination">
                            <span>共 <i style="color: #222;">{{total}}+</i> 商品</span>
                            <span><i style="color: red;">{{search.page}}</i>/{{totalPage}}</span>
                            <a class="btn-arrow" @click="prePage" style="display: inline-block">&lt;</a>
                            <a class="btn-arrow" @click="nextPage" style="display: inline-block">&gt;</a>
                        </div>
                    </div>
                </div>
                <div class="goods-list">
                    <ul class="yui3-g">
                        <li class="yui3-u-1-5" v-for="goods in goodsList" :key="goods.id">
                            <div class="list-wrap">
                                <div class="p-img">
                                    <a :href="'item/'+goods.id+'.html'" target="_blank"><img :src="goods.selected.image"
                                                                                             height="200"/></a>
                                    <ul class="skus">
                                        <!-- v-if 限制sku数量  -->
                                        <li :class="{selected: sku.id==goods.selected.id}" v-for="(sku,x) in goods.skus"
                                            v-if="x<6"
                                            :key="sku.id" @click="goods.selected=sku">
                                            <img :src="sku.image">
                                        </li>
                                    </ul>
                                </div>
                                <div class="clearfix"></div>
                                <div class="price">
                                    <strong>
                                        <em>¥</em>
                                        <i>{{ly.formatPrice(goods.selected.price)}}</i>
                                    </strong>
                                </div>
                                <div class="attr">
                                    <em>
                                        <a href="#" :title="goods.selected.title" style="color: black">{{goods.selected.title.length
                                            > 20 ? goods.selected.title.substring(0,20)+"...":goods.selected.title}}</a>
                                    </em>
                                </div>
                                <div class="cu">
                                    <em><span>促</span><a href="#" :title="goods.subTitle" style="color: black">{{goods.subTitle.length
                                        > 10 ? goods.subTitle.substring(0,10)+"...":goods.subTitle}}</a></em>
                                </div>
                                <div class="commit">
                                    <i class="command">已有2000人评价</i>
                                </div>
                                <div class="operate">
                                    <a href="success-cart.html" target="_blank" class="sui-btn btn-bordered btn-danger">加入购物车</a>
                                    <a href="javascript:void(0);" class="sui-btn btn-bordered">对比</a>
                                    <a href="javascript:void(0);"
                                       class="favorite"
                                       @click="addFavorite(goods)" v-if="goods.favoriteState===true">取消</a>
                                    <a href="javascript:void(0);"
                                       class="sui-btn btn-bordered"
                                       @click="addFavorite(goods)" v-else>关注</a>
                                </div>
                            </div>
                        </li>

                    </ul>
                </div>
                <div class="fr">
                    <div class="sui-pagination pagination-large">
                        <ul>
                            <li class="{prev:true,disabled:search.page === 1">
                                <a @click="prePage">«上一页</a>
                            </li>
                            <li class="{active : index(i) === search.page}" v-for="i in Math.min(5,totalPage)" :key="i">
                                <a @click="search.page=index(i)">{{index(i)}}</a>
                            </li>
                            <li class="{next:true,disable:search.page===totalPage}">
                                <a @click="nextPage">下一页»</a>
                            </li>
                        </ul>
                        <div><span>共{{totalPage}}页&nbsp;</span><span>
      到第
      <input type="text" class="page-num" v-model="currentPage">
      页 <button class="page-confirm" @click="goToPage()">确定</button></span></div>
                    </div>
                </div>
            </div>
            <!--hotsale-->
            <div class="clearfix hot-sale">
                <h4 class="title">热卖商品</h4>
                <div class="hot-list">
                    <ul class="yui3-g">
                        <li class="yui3-u-1-4">
                            <div class="list-wrap">
                                <div class="p-img">
                                    <img src="img/like_01.png"/>
                                </div>
                                <div class="attr">
                                    <em>Apple苹果iPhone 6s (A1699)</em>
                                </div>
                                <div class="price">
                                    <strong>
                                        <em>¥</em>
                                        <i>4088.00</i>
                                    </strong>
                                </div>
                                <div class="commit">
                                    <i class="command">已有700人评价</i>
                                </div>
                            </div>
                        </li>
                        <li class="yui3-u-1-4">
                            <div class="list-wrap">
                                <div class="p-img">
                                    <img src="img/like_03.png"/>
                                </div>
                                <div class="attr">
                                    <em>金属A面，360°翻转，APP下单省300！</em>
                                </div>
                                <div class="price">
                                    <strong>
                                        <em>¥</em>
                                        <i>4088.00</i>
                                    </strong>
                                </div>
                                <div class="commit">
                                    <i class="command">已有700人评价</i>
                                </div>
                            </div>
                        </li>
                        <li class="yui3-u-1-4">
                            <div class="list-wrap">
                                <div class="p-img">
                                    <img src="img/like_04.png"/>
                                </div>
                                <div class="attr">
                                    <em>256SSD商务大咖，完爆职场，APP下单立减200</em>
                                </div>
                                <div class="price">
                                    <strong>
                                        <em>¥</em>
                                        <i>4068.00</i>
                                    </strong>
                                </div>
                                <div class="commit">
                                    <i class="command">已有20人评价</i>
                                </div>
                            </div>
                        </li>
                        <li class="yui3-u-1-4">
                            <div class="list-wrap">
                                <div class="p-img">
                                    <img src="img/like_02.png"/>
                                </div>
                                <div class="attr">
                                    <em>Apple苹果iPhone 6s (A1699)</em>
                                </div>
                                <div class="price">
                                    <strong>
                                        <em>¥</em>
                                        <i>4088.00</i>
                                    </strong>
                                </div>
                                <div class="commit">
                                    <i class="command">已有700人评价</i>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

</div>

<script type="text/javascript">
    var vm = new Vue({
        el: "#searchApp",
        data: {
            ly,
            search: {
                key: "",
                page: 1,
                filter: {}
            },
            goodsList: [],
            total: 0,
            totalPage: 0,
            filters: [],
            show: false,
            breads: [],
            currentPage: 1,
        },
        components: {
            lyTop: () => import("./js/pages/top.js")
        },
        created() {
            //判断是否有请求参数
            if (!location.search) {
                return;
            }
            //将请求参数转换为对象
            const search = ly.parse(location.search.substring(1));
            //对Page进行初始化,防止第一次访问的时候page为空
            search.page = search.page ? parseInt(search.page) : 1;
            //对过滤对象进行初始化
            search.filter = search.filter || {};
            //对排序进行初始化,默认为"",代表不排序
            search.sortBy = search.sortBy || "";
            //对排序方式进行初始化,转换为布尔值
            search.descending = search.descending === "true" || false;
            //记录在data的search对象中
            this.search = search;
            //发起搜索请求
            this.loadData();
        },
        methods: {
            addFavorite(goods) {
                ly.verifyUser().then(res => {
                    //已关注,取消关注
                    if (goods.favoriteState) {
                        ly.http.delete("/favorite/" + goods.selected.id).then(res => {
                            goods.favoriteState = false;
                        })
                    } else {
                        //添加关注
                        ly.http.post("/favorite/" + goods.selected.id).then(res => {
                            //关注成功，？
                            goods.favoriteState = true;
                        })
                    }

                }).catch(() => {
                    window.location.href = "/login.html?returnUrl=" + location.href;
                })
            },
            goToPage() {
                this.search.page = this.currentPage;
            },
            deleteFilter(k) {
                /*this.search.filter={};*/
                Vue.delete(this.search.filter, k);
                //this.loadData();

            },
            getFilterValue(k, v) {
                //如果没有参数,跳过展示
                if (!this.filters || this.filters.length == 0) {
                    return null;
                }
                let filter = null;
                //判断是否是品牌
                if (k === '品牌') {
                    //返回品牌名称
                    return this.filters.find(f => f.k === '品牌').options[0].name;
                }
                return v;
            },
            selectFilter(k, o) {
                const obj = {};
                Object.assign(obj, this.search);
                if (k === '分类' || k === '品牌') {
                    o = o.id;
                }
                obj.filter[k] = o.name || o;
                this.search = obj;
            },
            prePage() {
                if (this.search.page > 1) {
                    return this.search.page--;
                }
            },
            nextPage() {
                if (this.search.page < this.totalPage) {
                    return this.search.page++;
                }
            },
            index(i) {
                if (this.search.page <= 3 || this.totalPage <= 5) {
                    return i;
                }
                ;
                if (this.search.page > this.totalPage - 2) {
                    return this.search.page - 5 + i;
                }
                if (this.search.page > 3) {
                    return this.search.page - 3 + i;
                }
                ;


            },
            loadData() {
                ly.http.post("/search/page", this.search).then(({data}) => {
                    this.filters = [];
                    //判断用户是否登录
                    // ly.verifyUser().then(res => {
                        data.items.forEach(goods => {
                            goods.skus = JSON.parse(goods.skus);

                            Object.assign(goods, {favoriteState: false});

                            //查询关注状态
                            ly.http.get("/favorite/state?id=" + goods.skus[0].id).then(boo => {
                                if (boo.data) {
                                    Object.assign(goods, {favoriteState: true})
                                }
                            })
                            goods.selected = goods.skus[0];
/*
                        })
                     }).catch(() => {
                         data.items.forEach(goods => {
                             goods.skus = JSON.parse(goods.skus);
                             goods.selected = goods.skus[0];

                             //默认关注状态 false
                             Object.assign(goods, {favoriteState: false});
                         })*/
                    });

                    this.totalPage = data.totalPage;
                    this.total = data.total;
                    this.goodsList = data.items;
                    this.currentPage = this.search.page;
                    if (data.categories.length == 1) {
                        ly.http.get("/item/category/all/level?id=" + data.categories[0].id)
                            .then(resp => this.breads = resp.data);
                    }
                    //初始化分类过滤项
                    this.filters.push({
                        k: "分类",
                        options: data.categories
                    });
                    this.filters.push({
                        k: "品牌",
                        options: data.brands
                    });

                    data.specs.forEach(spec => {
                        spec.options = spec.options.map(o => ({name: o}));
                        this.filters.push(spec);

                    })

                })

            }
        },
        watch: {
            search: {
                deep: true,
                handler(val, old) {
                    if (!old || !old.key) {
                        return;
                    }
                    window.location = "http://www.leyou.com/search.html?" + ly.stringify(this.search);
                }
            }
        }
    });
</script>
<!-- 底部栏位 -->
<!--页面底部，由js动态加载-->
<div class="clearfix footer"></div>
<script type="text/javascript">$(".footer").load("foot.html");</script>
<!--页面底部END-->

</body>
<!--购物车单元格 模板-->
<script type="text/template" id="tbar-cart-item-template">
    <div class="tbar-cart-item">
        <div class="jtc-item-promo">
            <em class="promo-tag promo-mz">满赠<i class="arrow"></i></em>
            <div class="promo-text">已购满600元，您可领赠品</div>
        </div>
        <div class="jtc-item-goods">
            <span class="p-img"><a href="#" target="_blank"><img src="{2}" alt="{1}" height="50" width="50"/></a></span>
            <div class="p-name">
                <a href="#">{1}</a>
            </div>
            <div class="p-price"><strong>¥{3}</strong>×{4}</div>
            <a href="#none" class="p-del J-del">删除</a>
        </div>
    </div>
</script>
<!--侧栏面板结束-->
<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#service").hover(function () {
            $(".service").show();
        }, function () {
            $(".service").hide();
        });
        $("#shopcar").hover(function () {
            $("#shopcarlist").show();
        }, function () {
            $("#shopcarlist").hide();
        });

    })
</script>
<script type="text/javascript" src="js/model/cartModel.js"></script>
<script type="text/javascript" src="js/czFunction.js"></script>
<script type="text/javascript" src="js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="js/widget/cartPanelView.js"></script>


</html>